package com.polaris.live.widget

import android.content.Context
import android.util.AttributeSet
import com.polaris.live.R
import com.polaris.live.widget.refresh.XRecyclerView

/**
 * 边缘半透明渐变效果
 *
 * @author Created by lucas on 2023/10/28 11:52
 * @since 1.0.0
 */
class FadingEdgeRecyclerView(context: Context, attributeSet: AttributeSet) : XRecyclerView(context, attributeSet) {

    private var mTopFadingEdge = false
    private var mBottomFadingEdge = false
    private var mLeftFadingEdge = false
    private var mRightFadingEdge = false

    init {
        val typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.FadingEdgeRecyclerView)
        mTopFadingEdge = typedArray.getBoolean(R.styleable.FadingEdgeRecyclerView_topFadingEdge, false)
        mBottomFadingEdge = typedArray.getBoolean(R.styleable.FadingEdgeRecyclerView_bottomFadingEdge, false)
        mLeftFadingEdge = typedArray.getBoolean(R.styleable.FadingEdgeRecyclerView_leftFadingEdge, false)
        mRightFadingEdge = typedArray.getBoolean(R.styleable.FadingEdgeRecyclerView_rightFadingEdge, false)
        typedArray.recycle()
    }

    override fun getTopFadingEdgeStrength(): Float {
        if (mTopFadingEdge) {
            return super.getTopFadingEdgeStrength()
        }
        return 0f
    }

    override fun getBottomFadingEdgeStrength(): Float {
        if (mBottomFadingEdge) {
            return super.getBottomFadingEdgeStrength()
        }
        return 0f
    }

    override fun getLeftFadingEdgeStrength(): Float {
        if (mLeftFadingEdge) {
            return super.getLeftFadingEdgeStrength()
        }
        return 0f
    }

    override fun getRightFadingEdgeStrength(): Float {
        if (mRightFadingEdge) {
            return super.getRightFadingEdgeStrength()
        }
        return 0f
    }

    fun setTopFadingEdge(topFadingEdge: Boolean) {
        mTopFadingEdge = topFadingEdge
        requestLayout()
    }

    fun setBottomFadingEdge(bottomFadingEdge: Boolean) {
        mBottomFadingEdge = bottomFadingEdge
        requestLayout()
    }

    fun setLeftFadingEdge(leftFadingEdge: Boolean) {
        mLeftFadingEdge = leftFadingEdge
        requestLayout()
    }

    fun setRightFadingEdge(rightFadingEdge: Boolean) {
        mRightFadingEdge = rightFadingEdge
        requestLayout()
    }
}